package com.huage_08.lesson_04;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ReceiveTest {
    public static void main(String[] args) throws IOException {
        //创建接收Socket对象
        DatagramSocket ds = new DatagramSocket(1997);

        String data = "";
        while (!data.equals("再见")){
            //接收数据包
            byte[] bytes = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
            ds.receive(dp);

            //解析数据
            byte[] datas = dp.getData();
             data = new String(datas,0,datas.length);
            System.out.println("接收到的数据为:" + data);
        }

        //关闭接收Socket对象
        ds.close();
    }
}
